/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gap.user;

import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.entities.MasterUser;
import javax.persistence.EntityManager;
import javax.security.auth.login.Configuration;

/**
 *
 * @author Yunita
 */
public class User extends Page{
    private Field userId;
    private Field userUname;
    private Field userPasswd;
    private Field userName;
    private Field userEmail;
    private Field userActive;
    private Field userDescription;
    
    public User() {
        super();
        setTheClass(MasterUser.class);
        userId = new Field();
        userId.setName("userId");
        userId.setCaption("id");
        userId.setListProperties(" align=\"center\" nowrap");
        userId.setInputProperties(" size=\"5\" readonly");
        getFields().add(userId);
        
        userUname = new Field();
        userUname.setName("userUname");
        userUname.setCaption("username");
        userUname.setListProperties(" width=\"10%\" nowrap");
        userUname.setInputProperties(" size=\"20\"");
        userUname.setUnique(true);
        userUname.setRequired(true);
        getFields().add(userUname);
        
        userPasswd = new Field();
        userPasswd.setName("userPasswd");
        userPasswd.setCaption("password");
        userPasswd.setListProperties(" width=\"15%\" nowrap");
        userPasswd.setInputProperties(" size=\"20\"");
        userPasswd.setInputType(Field.INPUT_PASSWORD);
        userPasswd.setDoubleEntry(true);
        userPasswd.setRequired(true);
        getFields().add(userPasswd);
        
        userName = new Field();
        userName.setName("userName");
        userName.setCaption("name");
        userName.setListProperties(" width=\"20%\" nowrap");
        userName.setInputProperties(" size=\"30\"");
        userName.setRequired(true);
        getFields().add(userName);
        
        userEmail = new Field();
        userEmail.setName("userEmail");
        userEmail.setCaption("email");
        userEmail.setListProperties(" width=\"20%\" nowrap");
        userEmail.setInputProperties(" size=\"30\"");
        userEmail.setRequired(true);
//        userEmail.setInputTypeHtml5("email");
        getFields().add(userEmail);
        
        userActive = new Field();
        userActive.setName("userActive");
        userActive.setCaption("active");
        userActive.setListProperties(" width=\"5%\" align=\"center\" nowrap");
        userActive.setInputProperties(" size=\"30\"");
        userActive.setRequired(true);
        userActive.setInputType(Field.INPUT_RADIO_BOX);
        userActive.getOptions().add(new String[]{"1", "Yes"});
        userActive.getOptions().add(new String[]{"2", "No"});
        getFields().add(userActive);
        
        userDescription = new Field();
        userDescription.setName("userDescription");
        userDescription.setCaption("description");
        userDescription.setListProperties(" width=\"30%\" ");
        userDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        userDescription.setInputType(Field.INPUT_TEXT_AREA);
//        userDescription.setUseRichEditor(true);
        getFields().add(userDescription);
        
        fillFields();
        
        getFieldsAdd().remove(userId);
        getFieldsEdit().remove(userId);
        getFieldsList().remove(userId);
        
        setController(new UserController(this));
        getController().setOrder(userName);
        getFieldsUnique().add(new Field[]{userUname});
        getFieldsUnique().add(new Field[]{userEmail});
    }

    @Override
    public String validate(Object data) {
        MasterUser users = (MasterUser) data;
        boolean result = true;
        String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
           java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
           java.util.regex.Matcher m = p.matcher(users.getUserEmail());
           System.out.println(m.matches() +" :Tesssss");
         if (m.matches() != result) {
            return Dictionary.getValue(this, "Format email salah ");
        }

        return null;
    }
    
    class UserController extends Controller {
        
        public UserController(Page page) {
            super(page);
        }
        
        @Override
        public void beforeInsert(Object data) {
            MasterUser mu = (MasterUser) data;
            mu.setUserPasswd(Utility.md5(mu.getUserPasswd()));
            mu.setUserSessionTimeout(3600);
        }
        
        @Override
        public void afterUpdate(Object data) {
            MasterUser mu = (MasterUser) data;
            if (mu.getUserPasswd().length() != 32) {
//                try {
//                    if (Configuration.getBoolean("subversion.sync")) {
//                        Subversion subversion = new Subversion();
//                        subversion.setPassword(mu.getUserUname(), mu.getUserPasswd());
//                    }
//                } catch (Exception e) {
//                }
                EntityManager em = getEntityManager();
                try {
                    em.getTransaction().begin();
                    System.out.println("--> "+"UPDATE MasterUser t SET t.userPasswd='" + Utility.md5(mu.getUserPasswd()) + "' WHERE t.userId='" + mu.getUserId() + "'");
                    em.createQuery("UPDATE MasterUser t SET t.userPasswd='" + Utility.md5(mu.getUserPasswd()) + "' WHERE t.userId='" + mu.getUserId() + "'").executeUpdate();
                    em.getTransaction().commit();
                } catch (Exception e) {
                    em.getTransaction().rollback();
                }
            }
        }
    }
}
